# helpers.py

#cf_threshold = 0.2
cf_threshold = 0.0

def sum_cf(*scores):
    r'''Sums the CF from each score and divides by the sum of the max
    scores.

    Also calculates the weight for each non None question as the max score
    in the score containing the question divided by the sum of the max
    scores.

    Each score is: (cf, max_score, question (or None))

    Result is: (cf, ((question, weight), ...))
    '''
    basis = sum(score[1] for score in scores)
    cf = sum(score[0] for score in scores) / basis
    return cf, tuple((question, max_score / basis)
                     for _, max_score, question in scores
                     if question is not None)

def mult_cf(*sums):
    r'''Multiplies the CF in the sums.

    This also recalculates the weight of the questions in each of the sums
    by multiplying by the product of all of the other CF values.  All of
    the questions from the separate sums are then combined into one result
    tuple.

    This returns an answer of the same form as each of the sums, which is:
        (cf, ((question, weight), ...))
    '''
    product = reduce(lambda prod, sum: prod * sum[0], sums, 1.0)
    return product, \
           tuple((question, weight * product / cf if cf else weight)
                 for cf, questions in sums
                 for question, weight in questions)

